package com.wei.czz.framework.admin.manager;

import com.wei.czz.common.constant.Constant;
import com.wei.czz.common.constant.EntityConstant;
import com.wei.czz.common.constant.RedisConstant;
import com.wei.czz.common.dto.admin.UserDto;
import com.wei.czz.common.dto.admin.deptUser.DeptUserPageEnumDto;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.CacheKeyUtils;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.admin.deptUser.DeptUserFormVo;
import com.wei.czz.framework.admin.entity.DeptUserEntity;
import com.wei.czz.framework.admin.entity.UserEntity;
import com.wei.czz.framework.admin.service.DeptUserService;
import com.wei.czz.framework.admin.service.UserService;
import com.wei.czz.framework.common.helper.CacheHelper;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-04-28 21:16:56
 * className: DeptUserManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class DeptUserManager {

    private static final Logger log = LoggerFactory.getLogger(DeptUserManager.class);

    private final DeptUserService deptUserService;

    private final UserService userService;

    private final CacheHelper cacheHelper;

    public List<UserDto> getSimpleDeptUserList(Long deptId) {

        /*
            获取部门关联用户
         */
        List<DeptUserEntity> deptUserList = deptUserService.findList(Collections.singletonList(deptId), null);
        // 收集用户主键
        List<Long> userIdList = CopyUtils.mapList(deptUserList, DeptUserEntity::getUserId);

        /*
            获取用户
         */
        List<UserEntity> userList = userService.getList(userIdList);

        Function<UserEntity, UserDto> function = (user) -> {
            UserDto userDto = new UserDto();
            userDto.setUserId(user.getUserId().toString())
                    .setUsername(user.getUsername())
                    .setStatus(user.getStatus())
                    .setStatusName(CommonEnum.ZERO.getValue().equals(user.getStatus()) ? "可用" : "不可用");
            return userDto;
        };
        // 列表映射
        return CopyUtils.mapList(userList, function);
    }

    public DeptUserPageEnumDto getDeptUserPageEnum(Long deptId) {
        // 获取请求用户主键
        Long optUserId = SecurityUtils.getUserId();

        /*
            获取用户请求参数
         */
        String cacheKey = CacheKeyUtils.getDeptUserParamCacheKey(deptId);
        DeptUserFormVo deptUserFormVo = cacheHelper.get(cacheKey, DeptUserFormVo.class);

        List<NameValue<Integer>> leaderStatusList = new ArrayList<>(2);
        leaderStatusList.add(new NameValue<>("是", CommonEnum.ZERO.getValue()));
        leaderStatusList.add(new NameValue<>("否", CommonEnum.ONE.getValue()));

        DeptUserPageEnumDto deptUserPageEnumDto = new DeptUserPageEnumDto();
        deptUserPageEnumDto.setStatusList(CommonEnum.getStatusList())
                .setDeptUserFormVo(deptUserFormVo)
                .setLeaderStatusList(leaderStatusList);
        return deptUserPageEnumDto;
    }
}
